package com.live.proxy;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.live.proxy.home.IntegralListActivity;
import com.live.proxy.home.IntegralPayActivity;
import com.live.proxy.home.ProxyManageActivity;
import com.live.proxy.home.SettingActivity;
import com.live.proxy.home.ThaliListActivity;
import com.live.proxy.home.UserManageActivity;
import com.live.proxy.model.UserModel;
import com.live.proxy.sql.UserDatabase;
import com.live.proxy.tool.GetUtil;
import com.live.proxy.tool.OkHttpUtils;
import com.live.proxy.tool.StatusBarUtils;
import com.live.proxy.view.ToastUtils;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class HomeActivity extends BaseActivity {
    TextView home_nickname;
    TextView home_integral_num;
    TextView home_auto_type;
    TextView home_auto_app;
    TextView home_proxy_list;
    TextView home_user_list;
    TextView home_integral_list;
    TextView home_setting;
    TextView home_thali_list;
    TextView home_integral_recharge;
    UserDatabase userDb;
    UserModel userModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        StatusBarUtils.setDarkStatusWhite(this,R.color.white);

        userDb = new UserDatabase(getApplicationContext());

        home_nickname = findViewById(R.id.home_nickname);
        home_integral_num = findViewById(R.id.home_integral_num);
        home_auto_type = findViewById(R.id.home_auto_type);
        home_auto_app = findViewById(R.id.home_auto_app);
        home_proxy_list = findViewById(R.id.home_proxy_list);
        home_user_list = findViewById(R.id.home_user_list);
        home_integral_list = findViewById(R.id.home_integral_list);
        home_setting = findViewById(R.id.home_setting);
        home_thali_list = findViewById(R.id.home_thali_list);
        home_integral_recharge = findViewById(R.id.home_integral_recharge);

        home_proxy_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), ProxyManageActivity.class));
            }
        });
        home_user_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), UserManageActivity.class));
            }
        });
        home_integral_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), IntegralListActivity.class));
            }
        });
        home_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), SettingActivity.class));
            }
        });
        home_thali_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), ThaliListActivity.class));
            }
        });
        home_integral_recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), IntegralPayActivity.class));
            }
        });
    }
    //设置账户
    public void setUserInfo(){
        userModel = userDb.select();
        if(userModel.getDirectType() == 1){
            home_integral_recharge.setVisibility(View.VISIBLE);
        }else{
            home_integral_recharge.setVisibility(View.GONE);
        }
        home_nickname.setText(userModel.getNickname());
        home_auto_type.setText(userModel.getAutoType() == 1?"代理":"贴牌");
        home_auto_app.setText(userModel.getAppName());

        //获取积分余额
        JSONObject appJson = GetUtil.getAppInfo(getApplicationContext());
        getAccount(GetUtil.getInteYue(),appJson.toString(),userModel.getToken());
    }
    @Override
    protected void onResume() {
        super.onResume();
        setUserInfo();//设置账户
    }
    public static final int RRQUEST_FAILURE = 1;
    public static final int RRQUEST_SUCCESS = 2;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RRQUEST_FAILURE:
                    ToastUtils.Setinfo(getApplicationContext(),msg.obj.toString(),2);
                    break;
                case RRQUEST_SUCCESS:
                    try {
                        JSONObject json = new JSONObject(msg.obj.toString());
                        if(json.getInt("code") == 1){
                            userDb.updateInteYue(json.getInt("data"));
                            userModel.setInteYue(json.getInt("data"));
                            home_integral_num.setText(String.valueOf(json.getInt("data")));
                        }else{
                            ToastUtils.Setinfo(getApplicationContext(),json.getString("info"),2);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
    public void getAccount(String url,String jsonStr,String token){
        OkHttpUtils.sendOkHttpJsonTokenRequest(url, jsonStr,new okhttp3.Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Message message = new Message();
                message.what = RRQUEST_FAILURE;
                message.obj = "【请求异常】请联系管理员";
                handler.sendMessage(message);
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String responseData = response.body().string();
                Message message = new Message();
                message.what = RRQUEST_SUCCESS;
                message.obj = responseData;
                handler.sendMessage(message);
            }
        },token);
    }
}